﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace SoilProfiler
{

    public class Camera
    {
        private Vector3 position;
        private Vector3 target;
        private Vector3 up;
        public Matrix4 ViewMatrix;
        public Matrix4 ProjectionMatrix;

        private float yaw, pitch;


        public Camera(float x, float y, float z)
        {
            target = new Vector3(x, y, z);
            up = Vector3.UnitZ;

           // target = -Vector3.UnitZ;
          //  up = Vector3.UnitY;
            ViewMatrix = Matrix4.Identity;
        }



        public Camera()
        {
            target = -Vector3.UnitZ;
            up = Vector3.UnitY;
            ViewMatrix = Matrix4.Identity;
        }

        public void Move(float x, float y, float z)
        {
            position.X = x;
            position.Y = y;
            position.Z = z;
        }

        public void Turn(float x, float y)
        {
            pitch += y;
            yaw += y;

            //ViewMatrix *= Matrix4.CreateFromAxisAngle(Vector3.UnitY, yaw);
            //				Yaw += y;
            //				
            //				rotationMatrix = Matrix4.CreateRotationY(Yaw);
            //				rightDirection = Vector3.Transform(rightDirection, rotationMatrix);
            //				lookDirection = Vector3.Transform(lookDirection, rotationMatrix);
        }

        public void Update()
        {
            GL.MatrixMode(MatrixMode.Modelview);
            ViewMatrix = Matrix4.LookAt(position, target, up);
            GL.LoadMatrix(ref ViewMatrix);
        }

        public void OnResize(int width, int height)
        {
            GL.Viewport(0, 0, width, width);
            ProjectionMatrix = Matrix4.CreatePerspectiveFieldOfView(
                (float)Math.PI / 4, (float)height / width, 1f, 1000000f);

            //				ProjectionMatrix = Matrix4.CreatePerspectiveFieldOfView(
            //					(float)Math.PI / 4, width / (float)height, 1f, 100f);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref ProjectionMatrix);
        }
    }


}
